package borge.web.teamspirit.client.page.team.widgets;

import borge.web.teamspirit.client.SessionData;
import borge.web.teamspirit.client.TeamSpirit;
import borge.web.teamspirit.client.TeamSpiritConstants;
import borge.web.teamspirit.client.data.Match;
import borge.web.teamspirit.client.data.Serie;
import borge.web.teamspirit.client.interfaces.WidgetEventHandler;
import borge.web.teamspirit.client.interfaces.WidgetEventHandler.WidgetEventType;
import borge.web.teamspirit.client.page.shared.widgets.CreateDataWidget;
import borge.web.teamspirit.client.page.shared.widgets.InputWrapper;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;

public class CreateMatchWidget extends CreateDataWidget implements ClickHandler {

    private Match iMatch;
    
    public CreateMatchWidget(WidgetEventHandler aEventHandler){
        super(aEventHandler);
        
        iPanel.add(iTable);
        initWidget(iPanel);
    }
    
    private void createDataTable(){
    	int row = 0;
        final int maxLength = 30;
        TeamSpiritConstants lcs = TeamSpirit.getConstants();
        
        // We want to populate a list with series to choose from, newest first in list
        // Let item text be Season Division, and value be serie id

        if(iMatch == null)
        	iMatch = new Match();
        
        addTableRow(row++,new InputWrapper(lcs.opponents(), createTextBox(iMatch.getOpponents(), maxLength), true));
        
        addTableRow(row++, new InputWrapper(lcs.date(), createDateBox(iMatch.getDateTime()), true));
        addTableRow(row++, new InputWrapper(lcs.gathering_time(), createDateBox(iMatch.getGatheringTime()), false));
        addTableRow(row++, new InputWrapper(lcs.city(), createTextBox(iMatch.getCity(), maxLength), false));
        addTableRow(row++, new InputWrapper(lcs.stadium(), createTextBox(iMatch.getStadium(), maxLength), false));
        addTableRow(row++, new InputWrapper(lcs.field(), createTextBox(iMatch.getField(), maxLength), false));
        addTableRow(row++, new InputWrapper(lcs.periods(), createTextBox(iMatch.getPeriods(), maxLength), false));
        addTableRow(row++, new InputWrapper(lcs.period_length(), createTextBox(iMatch.getPeriodLengthInMinutes(), maxLength), false));
        
        addTableRow(row++, new InputWrapper(lcs.our_score(), createTextBox(iMatch.getOurScore(), maxLength), false));
        addTableRow(row++, new InputWrapper(lcs.opponent_score(), createTextBox(iMatch.getOpponentScore(), maxLength), false));
        addTableRow(row++, new InputWrapper(lcs.cup_round(), createTextBox(iMatch.getCupRound(), maxLength), false));
        addTableRow(row++, new InputWrapper(lcs.other_info(), createTextArea(iMatch.getOtherInfo(), maxLength, 5), false));
        
        addButton(row, 0, this);
    }
    
    @SuppressWarnings("unchecked")
    public Match getDataObject(){
        return iMatch;
    }
    
    public void onClick(ClickEvent aEvent){
        
        postEvent(WidgetEventType.MATCH_CREATED);
        
    }
    
    @Override
	public void updateData() {
		
		iMatch = (Match)SessionData.sEditObject;
		SessionData.sEditObject = null;
		createDataTable();
	}
}
